local enshe = fk.CreateSkill{
  name = "miniex__enshe",
}

Fk:loadTranslationTable{
  ["miniex__enshe"] = "恩赦",
  [":miniex__enshe"] = "你的<a href='zhuluPindian'>逐鹿</a>点数+X（X为因此技能被你获得过牌的角色数）。" ..
    "每轮每名角色限一次，当你对一名角色造成伤害时，你可以防止此伤害，并获得其所有点数最大的手牌，然后其不能使用这些类别的牌直到其下回合开始。",

  ["#miniex__enshe-invoke"] = "你可对 %dest 发动〖恩赦〗，防止 %arg 点 %arg2 伤害，<br/>获得其所有点数最大的手牌，然后其不能使用这些类别的牌直到其下回合开始",
  ["@[list]miniex__enshe-round"] = "恩赦:轮",
  ["@[list]miniex__enshe_give"] = "恩赦:牌",
  ["@[cardtypes]miniex__enshe_prohibit"] = "恩赦 禁用",

  ["$miniex__enshe1"] = "陟罚与否，朕自有圣裁。",
  ["$miniex__enshe2"] = "卿之罪有失国体，岂能因前功而赦之？",
}

enshe:addEffect(fk.PindianCardsDisplayed, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(enshe.name) and player:getMark("@[list]miniex__enshe_give") ~= 0 and data.reason == "zhulu" then
      if player == data.from then
        return data.fromCard
      elseif data.results[player] then
        return data.results[player].toCard
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local num = #player:getMark("@[list]miniex__enshe_give")
    player.room:changePindianNumber(data, player, num, enshe.name)
  end,
})

enshe:addEffect(fk.DamageCaused, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(enshe.name) and not table.contains(player:getTableMark("@[list]miniex__enshe-round"), data.to)
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {
      skill_name = enshe.name,
      prompt = "#miniex__enshe-invoke::" .. data.to.id .. ":" .. data.damage .. ":" .. Fk:getDamageNatureName(data.damageType)
    }) then
      event:setCostData(self, { tos = {data.to} })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local to = data.to
    local room = player.room
    room:addTableMark(player, "@[list]miniex__enshe-round", to)
    data:preventDamage()
    if to ~= player then
      room:addTableMarkIfNeed(player, "@[list]miniex__enshe_give", to)
      local cards = {}
      local num = -1
      local types = {}
      for _, id in ipairs(to:getCardIds("h")) do
        local card = Fk:getCardById(id)
        if card.number >= num then
          if card.number > num then
            num = card.number
            cards = {}
            types = {}
          end
          table.insert(cards, id)
          table.insertIfNeed(types, card.type)
        end
      end
      room:obtainCard(player, cards, false, fk.ReasonPrey, player, enshe.name)
      if #types > 0 then
        room:setPlayerMark(to, "@[cardtypes]miniex__enshe_prohibit", types)
      end
    end
  end
})

enshe:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@[cardtypes]miniex__enshe_prohibit") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@[cardtypes]miniex__enshe_prohibit", 0)
  end
})

enshe:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if player:getMark("@[cardtypes]miniex__enshe_prohibit") ~= 0 then
      return table.contains(player:getMark("@[cardtypes]miniex__enshe_prohibit"), card.type)
    end
  end
})

return enshe
